package LinkedList;

public class 链表中的节点倒着每k个一组翻转 {
    public static void main(String[] args) {
        /**
         * 倒着每k个一组反转
         * 取余是前面不需要逆转的
         * 剩下的按照原来的方法逆转即可
         * */
        int[] nums = {1, 2, 3, 4, 5, 6, 7};
        ListNode listNode = ListNode.generateList(nums);
        int n = 7;
        int k = 2;
        int div = n%2;
        ListNode res = new ListNode(0);
        ListNode temp = res;
        ListNode tail = listNode;
        for (int i = 0; i < div - 1; i++) {
            tail = tail.next;
        }
        ListNode newHead = tail.next; //新头
        tail.next = null;  //截断第一段不需要反转的
        temp.next = listNode;  //连接
        while(temp!=null && temp.next!=null){
            temp = temp.next;
        }
        reverseKGroup reverseKGroup = new reverseKGroup();
        //后面的每k个逆转然后进行连接
        temp.next = reverseKGroup.reverseKGroup(newHead, k);
        ListNode.displayList(res.next);

    }
}
